import CustomAssemblerV4 from "./CustomAssemblerV4";

const { ccclass, property, menu } = cc._decorator;

@ccclass
@menu('CustomRenderV4/CustomRenderV4')
export default class CustomRenderV4 extends cc.RenderComponent {

    /** 纹理 */
    @property()
    private _texture: cc.Texture2D = null;

    @property({ type: cc.Texture2D, tooltip: '纹理' })
    public get texture(): cc.Texture2D {
        return this._texture;
    }
    public set texture(value: cc.Texture2D) {
        if (this._texture === value) { return; }
        this._texture = value;

        this.node.width = this._texture.width;
        this.node.height = this._texture.height;
        this.resetVerts();
    }

    @property()
    _vertXCount: number = 3;
    @property({ step: 1, min: 2, max: 20, tooltip: '上下边顶点数量(x 方向上)' })
    public get vertXCount() {
        return this._vertXCount;
    }
    public set vertXCount(value) {
        if (this._vertXCount === value) { return; }

        if (!this._vertXCount) this._vertXCount = 2;
        this._vertXCount = value;
        this.resetVerts();
    }

    @property()
    _vertYCount: number = 2;
    @property({ step: 1, min: 2, max: 20, tooltip: '上下边顶点数量(y 方向上)', })
    public get vertYCount() {
        return this._vertYCount;
    }
    public set vertYCount(value) {
        if (this._vertYCount === value) { return; }

        this._vertYCount = value;
        this.resetVerts();
    }

    gridVertexArray: cc.Vec2[];

    protected _initedMaterial: boolean = false

    onEnable() {
        super.onEnable();
        this.init();

        if (!this._assembler) { return; }
        //@ts-ignore
        this._assembler.updateRenderData(this);
    }

    /**
     * @dec 更新材质  
     * @dec 设置顶点数据修改 `setVertsDirty()`
     */
    public init() {
        if (!this._initedMaterial) {
            this.updateMaterial();
        }
        this.gridVertexArray = [];
        this.setVertsDirty();
    }

    /**
     * @dec override
     * @dec 在父类 `CCRenderCompoent` 的 `__proload` 中会被调用
     */
    public _resetAssembler() {
        let assembler = this._assembler = new CustomAssemblerV4();
        assembler.init(this);
    }

    /**
     * @dec Update material properties
     * @dec 设置Effect(builtin-2d-sprite.effect)里面使用的纹理属性
     * @dec 或者用户自定的纹理属性
     */
    protected _updateMaterial() {
        let material = this.getMaterial(0);
        if (material) {
            // TODO define
            // pos = cc_matViewProj * cc_matWorld * pos;
            material.define('CC_USE_MODEL', 1);

            // default effect 设置的属性
            material.setProperty('texture', this.texture);
        }
    }

    /**
     * @dec 更新材质
     */
    protected updateMaterial() {
        this._updateMaterial();
        this._initedMaterial = true;
    }

    private resetVerts() {
        //@ts-ignore
        this._assembler.updatePoint(this);

        //@ts-ignore
        this._assembler.initData();

        // this._initedMaterial = false;
        this.init();
        if (!this._assembler) { return; }
        //@ts-ignore
        this._assembler.updateRenderData(this);

        //@ts-ignore
        this._assembler.updateColor(this, null);
    }

    public update(dt) {
        this.gridVertexArray = [];
        //@ts-ignore
        let renderData = this._assembler._renderData;

        for (let i = 0; i < renderData.vDatas[0].length; i += 5) {
            let vx = renderData.vDatas[0][i];
            let vy = renderData.vDatas[0][i + 1];
            //
            this.gridVertexArray.push(cc.v2(vx, vy));
        }
        // cc.log(this.gridVertexArray);
    }
}